<?php

namespace app\common\model\car;

use app\admin\model\usedcar\Car;
use app\common\model\User;
use think\Cache;
use think\Model;

/**
 * 留资订单
 */
class CarUserLciOrderModel extends Model
{

    // 表名
    protected $name = 'car_user_lci_order';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = '';
    // 追加属性
    protected $append = [
        'createtime_text'
    ];

    // 获取订单号
    public static function getSn($user_id)
    {
        $rand     = $user_id < 9999 ? mt_rand(100000, 99999999) : mt_rand(100, 99999);
        $order_sn = date('Yhis') . $rand;

        $id = str_pad($user_id, (24 - strlen($order_sn)), '0', STR_PAD_BOTH);

        return $order_sn . $id;
    }
    public function car(){
        return $this->hasOne(CarUserLciModel::class,'id','user_lci_id');
    }
    public function user(){
        return $this->hasOne(User::class,'id','user_id')->field('id,avatar');
    }

    public function getCreatetimeTextAttr($value,$data)
    {
        if($data['createtime']){
            $value = date('Y-m-d H:i:s',$data['createtime']);
        }
        return $value;
    }
}
